﻿using BaseDll;
using CommonTools;
using FinalTest_Algonrithm;
using MotionIoLib;
using Newtonsoft.Json;
using Sunny.UI;
using System;
using System.ComponentModel;
using System.Threading;
using UserData;

namespace StationDemo
{
    public class Param_Table
    {
    }
    public class Point_Table
    {
        public PointInfo 工位A上料位 = new PointInfo(0, 0, 0, 0, 0, 0, UserRight.Operator, "综合");
        public PointInfo 工位B上料位 = new PointInfo(0, 0, 0, 0, 0, 0, UserRight.Operator, "综合");
    }

    public class Config_Table
    {
    }

    public enum TABLESTATUS
    {
        stop,
        ready_clibra,
        ready_load,
        ing
    }


    public class Station_Table : CommonTools.StationbaseEx<Param_Table, Point_Table, Config_Table>
    {
        #region 变量

        public TABLESTATUS TableStatus { get; private set; } = TABLESTATUS.stop;
        public StationInfor loadStation = new StationInfor(SOCKET.SOCKETA);
        public StationInfor clibraStation = new StationInfor(SOCKET.SOCKETB);

        #endregion

        public Station_Table(CommonTools.Stationbase station, Param_Table param, Point_Table point, Config_Table config) : base(station, param, point, config)
        {

        }

        public enum StationStep
        {
            [Description("0.初始化步骤")]
            Step_Init = 100,
            [Description("1.等待转盘旋转信号")]
            Step_等待转盘旋转信号,
            [Description("3.结束流程")]
            Step_End,
            [Description("4.NG停止流程")]
            Step_NG,
        }




        #region 主流程
        protected override void StationWork(int step)
        {
            try
            {
                switch (step)
                {
                    case (int)StationStep.Step_Init:
                        DelCurrentStep();
                        PushMultStep((int)StepInitRun());
                        break;
                    case (int)StationStep.Step_等待转盘旋转信号:
                        DelCurrentStep();
                        PushMultStep((int)Step_等待转盘旋转信号());
                        break;
                    case (int)StationStep.Step_End:
                        DelCurrentStep();
                        PushMultStep((int)StepEnd());
                        Thread.Sleep(10);
                        break;
                }

            }
            catch (Exception ex)
            {
                LogHelper.OnTest_ShowLog(Name, $"{Name} 发生错误:{ex.StackTrace},{ex.Message}", LogType.Err);
                StationMgr.GetInstance().Stop();
            }
        }

        protected override bool InitStation()
        {
            ClearAllStep();
            PushMultStep((int)StationStep.Step_Init);
            this.loadStation = new StationInfor(SOCKET.SOCKETA);
            this.clibraStation = new StationInfor(SOCKET.SOCKETB);


            return true;
        }
        #endregion


        #region 方法

        public StationStep StepInitRun(bool bmanual = false)
        {
            LogHelper.OnTest_ShowLog(Name, $"转盘站，初始化", LogType.Warn);
            LogHelper.OnTest_ShowLog(Name, $"转盘站，等待工位信号");
            return StationStep.Step_等待转盘旋转信号;
        }

        public StationStep Step_等待转盘旋转信号(bool bmanual = false)
        {
            if (TableStatus == TABLESTATUS.ing)
            {
                LogHelper.OnTest_ShowLog(Name, $"转盘站，接收到转动信号", LogType.Warn);
                return StationStep.Step_End;
            }
            return StationStep.Step_等待转盘旋转信号;
        }

        public StationStep StepEnd(bool bmanual = false)
        {

            double u = loadStation.socket == SOCKET.SOCKETA ? station_Point.工位B上料位.pointU : station_Point.工位A上料位.pointU;
            MoveSigleAxisPosWaitInpos(AxisU, u, (double)SpeedType.High, 0.1, bmanual);
            Thread.Sleep(10);
            ExchangeStationInfor();
            this.TableStatus = TABLESTATUS.stop;
            return StationStep.Step_等待转盘旋转信号;
        }

        public bool Function_工站回零()
        {
            loadStation = new StationInfor(SOCKET.SOCKETA);
            clibraStation = new StationInfor(SOCKET.SOCKETB);
            UserDefineFunction.Axis_回零(AxisU, "AxisU", this, true);
            return true;
        }

        private void ExchangeStationInfor()
        {
            // 先交换数据
            string temp = JsonConvert.SerializeObject(loadStation);
            loadStation = JsonConvert.DeserializeObject<StationInfor>(JsonConvert.SerializeObject(clibraStation));
            clibraStation = JsonConvert.DeserializeObject<StationInfor>(temp);

        }

        public string Function_GetProduct(Station station)
        {
            if (station == Station.上料站)
                return JsonConvert.SerializeObject(loadStation);
            else
                return JsonConvert.SerializeObject(clibraStation);
        }

        // 其他工站通知转盘已经准备完成
        public void Function_NotifyTestEnd(Station station, string productJson)
        {
            if (station == Station.上料站)
            {
                LogHelper.OnTest_ShowLog(Name, $"收到上料站通知", LogType.Warn);

                var st = JsonConvert.DeserializeObject<StationInfor>(productJson);
                st.socket = loadStation.socket;
                loadStation = st;
            }
            else
            {
                LogHelper.OnTest_ShowLog(Name, $"收到标定站通知", LogType.Warn);
                var st = JsonConvert.DeserializeObject<StationInfor>(productJson);
                st.socket = clibraStation.socket;
                clibraStation = st;
            }

            TABLESTATUS temp = TableStatus;
            if (this.TableStatus == TABLESTATUS.stop)
            {
                this.TableStatus = station == Station.上料站 ? TABLESTATUS.ready_load : TABLESTATUS.ready_clibra;
            }
            else if (TableStatus == TABLESTATUS.ready_load || TableStatus == TABLESTATUS.ready_clibra)
                this.TableStatus = TABLESTATUS.ing;

            LogHelper.OnTest_ShowLog(Name, $"状态:[{temp}]->[{TableStatus}]");
        }


        public void Function_TurnTable(Station station)
        {
            if (station == Station.上料站)
            {
                if (loadStation.socket != SOCKET.SOCKETA)
                {
                    MoveSigleAxisPosWaitInpos(AxisU, station_Point.工位A上料位.pointU, (double)SpeedType.High, 0.1, true);
                    ExchangeStationInfor();
                }
            }
            else
            {
                if (loadStation.socket != SOCKET.SOCKETB)
                {
                    MoveSigleAxisPosWaitInpos(AxisU, station_Point.工位B上料位.pointU, (double)SpeedType.High, 0.1, true);
                    ExchangeStationInfor();
                }
            }
        }


        public (string capBoxSN, Station_治具 station_治具) Function_Get硬件信息(Station station = Station.标定站)
        {
            Station_Load station_load = (Station_Load)StationMgr.GetInstance().GetStation(Station.上料站.ToString());
            StationInfor si = station == Station.标定站 ? this.clibraStation : this.loadStation;

            Station_治具 station_治具 = si.socket == SOCKET.SOCKETA ?
                (Station_治具)StationMgr.GetInstance().GetStation(Station.治具A站.ToString()) :
                (Station_治具)StationMgr.GetInstance().GetStation(Station.治具B站.ToString());

            string capBoxSN = si.socket == SOCKET.SOCKETA ? station_load.station_Config.str_StationACapBoxSN : station_load.station_Config.str_StationBCapBoxSN;
            return (capBoxSN, station_治具);
        }


        #endregion


    }
}
